<?php

class BaseAction extends Action {

    static $config = null;
    protected $cacheKey = array();

    function _initialize() {
        $configs = M("Option")->select();
        import('@.ORG.Session');
        import('@.ORG.Cookie');
        foreach ($configs as $value) {
            self::$config[$value['type']] = $value['value'];
            $this->assign($value['type'], $value['value']);
        }
        
        $SaeLocationObj = new SaeLocation();
        $ip_to_geo_arr = array('ip'=> get_client_ip());
        $ip_to_geo = $SaeLocationObj->getIpToGeo($ip_to_geo_arr);
        $geo = $ip_to_geo['geos'][0];
        foreach ($geo as $key => $value) {
            $_SESSION[$key]=$value;
        }
        
       
//        $adress = D("Region")->getById(Cookie::get('city'));
//        if (empty($adress)) {
//            $this->redirect('/choose/', array(), 0, '');
//        }

        //$this->assign('city', $this->getCity());
        $this->assign("adress", $adress['name']);
    }

    public function verify() {
        $type = isset($_GET['type']) ? $_GET['type'] : 'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4, 1, $type, '', '24px');
    }

    /**
      +------------------------------------------------------------------------------
     * 公共 文件上传方法
      +------------------------------------------------------------------------------
     * $path    string  上传路径
     * $maxsize int     上传文件最大值
     * $thumb   boolean 是否生成缩略图
     * $width   int     缩略图最大宽度
     * $height  int     缩略图最大高度
     * $autosub boolean 是否使用子目录保存文件
      +------------------------------------------------------------------------------
     */
    public function _upload($path, $thumb = false, $width, $height, $autosub = false, $maxsize) {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        isset($maxsize) ? $upload->maxSize = $maxsize : $upload->maxSize = 1048576; //1M
        isset($path) ? $upload->savePath = $savepath = "./Attachments/" . $path . "/" : $upload->savePath = "./Attachments/Others/";
        if (!is_dir($savepath))
            @mk_dir($savepath);
        $upload->allowExts = explode(',', 'gif,png,jpg,jpeg');

        if ($autosub) {
            $upload->autoSub = true;
            $upload->subType = 'date';
            $upload->saveRule = time;
            $upload->dateFormat = 'Y/m/d';
        }
        if ($thumb) {
            $upload->thumb = true;
            $upload->thumbPrefix = '';
            $upload->thumbSuffix = '_thumb';
            isset($wideh) ? $upload->thumbMaxWidth = $width : $upload->thumbMaxWidth = "300";
            isset($height) ? $upload->thumbMaxHeight = $height : $upload->thumbMaxHeight = "400";
        }
        if (!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        } else {
            $imginfo = $upload->getUploadFileInfo();
            $imginfo = $imginfo[0]['savename'];
        }
        return $imginfo;
    }

    public function _getCate() {


        if (S('cate')) {
            $cat_value = S('cate');
        } else {
            include COMMON_PATH . 'tree.php';
            $Tree = new Tree('');
            $Categroy = D("Categroy");
            // 把查询条件传入查询方法
            $cat_value = $Categroy->order('fatherid asc')->select();

            foreach ($cat_value AS $id => $row) {
                $Tree->setNode($row['id'], $row['fatherid'], $row['name']);
            }
            $_cat = $Tree->getChilds();

            foreach ($_cat as $key => $id) {
                $cat_value[$key]['id'] = $id;
                $c_data = $Categroy->where('id=' . $id)->find();
                $cat_value[$key]['name'] = $Tree->getLayer($id, '&nbsp;') . $Tree->getValue($id);
                $cat_value[$key]['fatherid'] = $c_data['fatherid'];
                $cat_value[$key]['orderid'] = $c_data['orderid'];
                $cat_value[$key]['pinyin'] = $c_data['pinyin'];
            }
            S('cate', $cat_value, 100);
        }

        return $cat_value;
    }

    public function _getCateOption() {

        if (S('cateOption')) {
            $new_cat = S('cateOption');
        } else {
            include COMMON_PATH . 'tree.php';

            $Tree = new Tree('顶级分类');
            $Categroy = D("Categroy");
            $cat_value = $Categroy->order('fatherid asc')->select();

            foreach ($cat_value AS $id => $row) {
                $Tree->setNode($row['id'], $row['fatherid'], $row['name']);
            }

            $categorys = $Tree->getChilds();

            //遍历输出
            $new_cat = array();
            foreach ($categorys as $key => $id) {
                $new_cat[$id] = $Tree->getLayer($id, '&nbsp;') . $Tree->getValue($id);
            }

            S('cateOption', $new_cat);
        }
        return $new_cat;
    }

    // 删除操作时清空缓存
    public function _after_doadd() {
        $Cache = Cache::getInstance();
        foreach ($this->cacheKey as $value) {
            $Cache->rm($value);
        }
    }


    


    public function sendMail($mail) {
        $mail = D("Mail");
        $mail->to = $email['to'];
        $mail->subject = $mail['subject'];
        $mail->content = $mail['content'];
        $id = $mail->add();

        $mail = D("Mail");
        $mail = $mail->getById($id);

        $saemail = new SaeMail();
        $saemail->setOpt(array(
            'from' => 'notsupport',
            'to' => $mail['to'],
            'smtp_host' => 'smtp.gmail.com',
            'smtp_port' => '587',
            'smtp_username' => 'sunbo@quwanma.com',
            'smtp_password' => '19831023',
            'subject' => $mail['subject'],
            'content' => $mail['content'],
            'tls' => true
        ));

        $ret = $saemail->send();
        //发送失败时输出错误码和错误信息
        if ($ret) {
            $mail->status = 1;
            $mail->save();
        }
    }

    public function getLocation() {
        $SaeLocationObj = new SaeLocation();
        $ip_to_geo_arr = array('ip' => get_client_ip());
        $ip_to_geo = $SaeLocationObj->getIpToGeo($ip_to_geo_arr);
        return $ip_to_geo['geos'][0];
    }
    


}

?>